package com.huaxiafinance.management.controller;

import com.huaxia.middleware.sdk.utils.StringUtil;
import com.huaxiafinance.management.util.FileUtil;
import com.huaxiafinance.management.util.MessageResult;
import com.huaxiafinance.management.util.PropertiesLoader;
import com.huaxiafinance.management.util.UUIDUtil;
import com.qiniu.util.Auth;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by lenovo on 2017/4/21.
 */
@Controller("systemController")
@RequestMapping("/system")
public class SystemController {

  private static final Logger log = Logger.getLogger(SystemController.class);

  @RequestMapping(value = "/getQiNiuUploadToken", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getQiNiuUploadToken(String picName) throws Exception {

    log.info("获取七牛上传凭证请求参数" + picName);

    MessageResult messageResult = new MessageResult();

    if (StringUtil.isEmpty(picName)) {
      messageResult.setFailureResult("图片名称不能为空");
    } else {

      String ak = PropertiesLoader.getProperty("qiniu.accesskey");
      String sk = PropertiesLoader.getProperty("qiniu.secretkey");
      String bucket = PropertiesLoader.getProperty("qiniu.bucket");
      String domain = PropertiesLoader.getProperty("qiniu.domain");

      Auth auth = Auth.create(ak, sk);

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
      String dateFormat = sdf.format(new Date());
      String name =
          "images/" + dateFormat + "/" + UUIDUtil.getUUID() + FileUtil.getFileExtension(picName);

      String token = auth.uploadToken(bucket, name);

      HashMap<String, Object> map = new HashMap<>();
      map.put("token", token);
      map.put("key", name);
      map.put("domain", domain);
      messageResult.setData(map);
      messageResult.setSuccessResult();
    }

    log.info("获取七牛上传凭证返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

  @RequestMapping(value = "/getQiNiuUploadTokenSecret", produces = "application/json; charset=utf-8")
  public @ResponseBody
  String getQiNiuUploadTokenSecret(String picName) throws Exception {

    log.info("获取七牛私密上传凭证请求参数" + picName);

    MessageResult messageResult = new MessageResult();

    if (StringUtil.isEmpty(picName)) {
      messageResult.setFailureResult("图片名称不能为空");
    } else {

      String ak = PropertiesLoader.getProperty("qiniu.accesskey");
      String sk = PropertiesLoader.getProperty("qiniu.secretkey");
      String bucket = PropertiesLoader.getProperty("qiniu.secretBucket");
      String domain = PropertiesLoader.getProperty("qiniu.domain");

      Auth auth = Auth.create(ak, sk);

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
      String dateFormat = sdf.format(new Date());
      String name =
          "images/" + dateFormat + "/" + UUIDUtil.getUUID() + FileUtil.getFileExtension(picName);

      String token = auth.uploadToken(bucket, name);

      HashMap<String, Object> map = new HashMap<>();
      map.put("token", token);
      map.put("key", name);
      map.put("domain", domain);
      messageResult.setData(map);
      messageResult.setSuccessResult();
    }

    log.info("获取七牛私密上传凭证返回参数" + messageResult.getAsJSON());

    return messageResult.getAsJSON();
  }

}
